<template>
  <div class="params_container">
    <div class="header">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>参数列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card>
      <el-alert show-icon title="注意: 只允许为第三级分类设置相关参数！" type="warning" :closable="false"></el-alert>
      <el-row>
        <el-col :span="10" class="Text-cascader">
          <span>选择商品分类: </span>
          <!--级联选择 props:配置对象-->
          <el-cascader v-model="selectedCat_pid" :options="cateList" :props="cateProps" @change="cascaderChange"></el-cascader>
        </el-col>
      </el-row>
      <!--标签页-->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane :disabled="tabPaneDisabled" label="动态参数" name="many" align="left">
          <el-button type="primary" size="small" :disabled="tabPaneDisabled" @click="addParamsDialog">添加参数</el-button>
          <!--动态参数表格-->
          <el-table :data="manyTableData" style="width: 100%" border stripe>
            <!--展开行-->
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag closable v-for="(item,$index) in scope.row.attr_vals" @close="handleClose(scope.row,$index)"  :key="$index">{{item}}</el-tag>
                <!--显示最后一个添加标签输入框-->
                <el-input class="input-new-tag" v-if="scope.row.tagInputVisible" v-model="scope.row.tagInputValue"
                  ref="saveTagInput" size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm((scope.row))"></el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showTagInput(scope.row)">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="attr_name" label="参数名称"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteAttributesById(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane :disabled="tabPaneDisabled" label="静态属性" name="only" align="left">
          <el-button type="primary" size="small" :disabled="tabPaneDisabled" @click="addParamsDialogVisible=true">添加属性</el-button>

          <el-table :data="onlyTableData" style="width: 100%" border stripe>
            <!--展开行-->
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag closable v-for="(item,$index) in scope.row.attr_vals" @close="handleClose(scope.row,$index)" :key="$index">{{item}}</el-tag>
                <!--显示最后一个添加标签输入框-->
                <el-input class="input-new-tag" v-if="scope.row.tagInputVisible" v-model="scope.row.tagInputValue"
                          ref="saveTagInput" size="small"
                          @keyup.enter.native="handleInputConfirm(scope.row)"
                          @blur="handleInputConfirm((scope.row))"></el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showTagInput(scope.row)">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteAttributesById(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <!--添加动态参数和静态属性对话框-->
      <el-dialog :title="titleText1+titleText" align="left" :visible.sync="addParamsDialogVisible" width="35%" @close="addformReset">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" class="demo-ruleForm">
          <el-form-item :label="titleText" prop="attr_name">
            <el-input v-model="addForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <!--底部区-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addParamsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addParams">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  import connector from '../../base/interface.js';
    export default {
      name: "params",
      data(){
        return{
          /*商品分类列表*/
          cateList:[],
          /*级联选择框配置对象*/
          cateProps:{
            label:'cat_name',
            value:'cat_id',
            children:'children',
            expandTrigger:'hover',
            emitPath:true
          },
          /*级联选择框选中的值*/
          selectedCat_pid:[],
          /*当前选中的标签页名称*/
          activeName:'many',
          /*标签页是否禁用*/
          tabPaneDisabled:true,
          /*动态参数数据*/
          manyTableData:[],
          /*静态属性数据*/
          onlyTableData:[],
          /*添加动态参数和静态属性的对话框*/
          addParamsDialogVisible:false,
          /*添加参数或属性的表单*/
          addForm:{id:0,attr_name:'',attr_sel:'only',attr_vals:''},
          /*添加参数或属性的表单验证规则*/
          addFormRules:{attr_name:[{required:true,message:'请输入内容', trigger: 'blur'}],},
          /*标识当前对话框是处于添加还是修改 edit*/
          addOrEdit:'add',
          /*当前点击编辑的数据表单*/
          editForm:{}

        }
      },
      methods:{
        getCateList(){
          /*获取所有的商品分类列表*/
          connector.getCateList().then(res=>{
            var {data:data}=res;
            this.cateList=data.data;
          });
        },
        cascaderChange(){
          /*级联选择框值改变, 设置只允许选择第三级分类*/
          if(this.selectedCat_pid.length <=2){
            this.selectedCat_pid.length=[];
            this.tabPaneDisabled=true;
            /*清空表格数据*/
            this.manyTableData=[];
            this.onlyTableData=[];
            return;
          }
          this.getParamsData();
          this.tabPaneDisabled=false;
        },
        handleTabClick(tab){
          /*标签页切换触发函数*/
          this.getParamsData();
        },
        getParamsData(){
          /*获取参数列表数据*/
          connector.getCategories(this.getCatId,this.activeName).then(res=>{
            var {data:data}=res;
            // attr_vals 字段安空格分割为数组
            data.data.forEach(item=>{
              item.attr_vals=item.attr_vals?item.attr_vals.trim().split(/\s+/):[];
              /*当前新添加标签的值*/
              item.tagInputValue='';
              /*输入框与按钮的切换显示*/
              item.tagInputVisible=false;
            });

            if(this.activeName=="many"){
              /*动态参数*/
              this.manyTableData=data.data;
            }else{
              /*静态属性*/
              this.onlyTableData=data.data;
            }
          });
        },
        addformReset(){
          /*关闭对话框表单重置*/
          this.$refs.addFormRef.resetFields()
          this.addForm={id:0,attr_name:'',attr_sel:'only',attr_vals:''};
        },
        showEditDialog(row){
          /*点击编辑按钮触发*/
          connector.getAttributesById(this.getCatId,row.attr_id).then(res=>{
            var {data:data}=res;
            this.addForm.attr_name=data.data.attr_name;
            this.editForm=data.data;
            this.addOrEdit='edit';
            this.addParamsDialogVisible=true;
          });
        },
        addParamsDialog(){
          /*点击添加参数按钮触发*/
          this.addParamsDialogVisible=true;
          this.addOrEdit='add';
        },
        addParams(){
          /*添加/修改 动态参数或者静态属性*/
          this.$refs.addFormRef.validate(vaild=>{
            if(!vaild)return;
            var addParamsObj={id:this.getCatId,attr_name:this.addForm.attr_name,attr_sel:this.activeName};
            var editParamsObj={id:this.getCatId,attr_name:this.addForm.attr_name,attrId:this.editForm.attr_id,attr_sel:this.editForm.attr_sel};
            if(this.addOrEdit=='add'){
            //  添加
              connector.addParamsOrAttributes(addParamsObj).then(res=>{
                var {data:data}=res;
                this.$message({message:data.meta.msg,type:'success'});
              })
            }else{
            //  编辑
              connector.editAttributes(editParamsObj).then(res=>{
                var {data:data}=res;
                this.$message({message:data.meta.msg,type:'success'});
              })
            }
            /*重置并关闭*/
            this.$refs.addFormRef.resetFields();
            this.addParamsDialogVisible=false;
            this.getParamsData();

          });
        },
        deleteAttributesById(row){
          /*删除参数*/
          this.$confirm('此操作将删除该参数, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
          }).then(() => {
            connector.deleteAttributesById(this.getCatId,row.attr_id).then(res=>{
              /*删除成功*/
              var {data:data}=res;
              this.$message({message:data.meta.msg,type:'success'});
              this.getParamsData();
            });
          });
        },
        handleInputConfirm(row){
          /*添加新标签*/
          if(row.tagInputValue.trim().length != 0){
            row.attr_vals.push(row.tagInputValue);
            /*将新标签保存到服务器*/
            var editParamsObj={id:this.getCatId,attrId:row.attr_id,attr_name:row.attr_name,attr_sel:row.attr_sel,attr_vals:row.attr_vals.join(' ')};
            connector.editAttributes(editParamsObj).then(res=>{
              var {data:data}=res;
              this.$message({message:data.meta.msg,type:'success'});
              row.tagInputVisible=false;
              row.tagInputValue='';
            })
          }
        },
        handleClose(row,i){
          /*删除对应的参数可选项*/
          row.attr_vals.splice(i,1);
          var editParamsObj={id:this.getCatId,attrId:row.attr_id,attr_name:row.attr_name,attr_sel:row.attr_sel,attr_vals:row.attr_vals.join(' ')};
          connector.editAttributes(editParamsObj).then(res=>{
            var {data:data}=res;
            this.$message({message:data.meta.msg,type:'success'});
            row.tagInputVisible=false;
            row.tagInputValue='';
          })
        },
        showTagInput(row) {
          row.tagInputVisible = true;

          /*文本框自动获得焦点, nextTick: 页面上的元素重新渲染完之后回调*/
          this.$nextTick(_ => {this.$refs.saveTagInput.$refs.input.focus()});
        }
      },computed:{
        titleText1(){
          /*动态计算标题文本*/
          return this.addOrEdit=='add'?'添加':'修改';
        },
        titleText(){
          /*动态计算标题文本*/
          return this.activeName=='many'?'动态参数':'静态属性';
        },
        getCatId(){
          /*获取当前分类id*/
          return this.selectedCat_pid[this.selectedCat_pid.length-1];
        }
      },
      created() {
        this.getCateList();
      }
    }
</script>

<style lang="scss" scoped>
.params_container{
  .el-row{
    margin: 15px 0;
  }
  .Text-cascader{
    display: flex;
    justify-content: left;
    align-items: center;
    span{
      margin-right: 10px;
    }
  }
  .el-tag{
    margin: 0 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
